package com.wheesper.www.WheesperServer.Services;

import java.util.HashMap;
import java.util.Map;

import com.Wheesper.www.WheesperServer.Model.ContactsCenter;
import com.Wheesper.www.WheesperServer.Model.User;
import com.Wheesper.www.WheesperServer.Model.UserCenter;
import com.Wheesper.www.WheesperServer.Model.UserInfo;
import com.Wheesper.www.WheesperServer.Model.UserInfoCenter;
import com.wheesper.www.WheesperServer.Net.Session.IOSession;
import com.wheesper.www.WheesperServer.Utils.CaptchaGenerator;
import com.wheesper.www.WheesperServer.Utils.Logger;

import ProtocolBuffer.ProtoMessageOuterClass.*;

public class RegisterController {
	private Map<String,String> _email2captcha;
	/*
	private void response(ProtobufMessage msg,Channel channel) {
		
	}
	*/
	
	private static RegisterController _instance = new RegisterController();
	public static RegisterController Instance() {
		return _instance;
	}
	
	private RegisterController() {
		_email2captcha = new HashMap<String,String>();
	}
	
	//helper Method
	private void emailInvalidResponse(IOSession session,String email) {
		SignupMailResponse response = SignupMailResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(false)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setSignupMailResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	
	private void emailValidResponse(IOSession session,String email) {
		SignupMailResponse response = SignupMailResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(true)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setSignupMailResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	
	
	private void suceededRegisteredResponse(IOSession session,User user) {
		SignupInfoResponse response = SignupInfoResponse.newBuilder()
				.setMailAddress(user.eMail())
				.setStatus(true)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setSignupInfoResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
		_email2captcha.remove(user.eMail());
	}
	
	private void failedRegisteredResponse(IOSession session,String email) {
		SignupInfoResponse response = SignupInfoResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(false)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setSignupInfoResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	
	private void captchaSendedResponse(IOSession session,String email) {
		SignupCaptchaResponse response = SignupCaptchaResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(true)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setSignupCaptchaResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	
	private void registerRoutine(String email) {
		String captcha = CaptchaGenerator.genCatcha("register" + email);
		//create a registerRoutine State!
		_email2captcha.put(email, captcha);
		Logger.Instance().logging("[Register Routine Applying]{\n"
				+ "Email:"+email + "\n"
				+ "Captcha:" + captcha + "\n}");
	}
	
	
	public void registerApply(IOSession session,String email) {
		registerRoutine(email);
		//TODO:send Captcha
		captchaSendedResponse(session,email);
	}
	
	
	public void checkEmailExisted(IOSession session,String email) {
		if(UserCenter.Instance().checkEmail(email)) {
			emailValidResponse(session,email);
			
			Logger.Instance().logging("[Check Email Existed]{\n"
					+ "Email:" + email + "\n"
					+ "Existed?:false\n}");
		}else {
			emailInvalidResponse(session,email);
			
			Logger.Instance().logging("[Check Email Existed]{\n"
					+ "Email:" + email + "\n"
					+ "Existed?:true\n}");
		}
	}
	
	public boolean checkCaptcha(String email,String captcha) {
		String capt = _email2captcha.get(email);
		return (capt != null) && (capt.contentEquals(captcha));		
	}
	
	
	public void registering(IOSession session,String email,String password,String captcha,String nickname) {
		//Check the captcha mapping
		if(checkCaptcha(email,captcha)) {
			try {
				User user = UserCenter.Instance().register(email, password,nickname);
				suceededRegisteredResponse(session,user);
				
				Logger.Instance().logging("[Register Succussfully]{\n"
						+ "UserEmail:" + email + "\n"
						+ "UserNickName:" + nickname + "\n"
						+ "Password:" + password + "\n}");
				
			}catch(Exception e) {
				e.printStackTrace();
				emailInvalidResponse(session,email);
			}
		}else {
			failedRegisteredResponse(session,email);
			Logger.Instance().logging("[Register Failed]{\n"
					+ "UserEmail:" + email + "\n"
					+ "UserNickName:" + nickname + "\n"
					+ "Password:" + password + "\n"
					+ "Reason:Error Captcha  "+ captcha + "\n}");
			
		}
	}
}
